<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>注册</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    h1 {
      width: 1200px;
      height: 90px;
      margin: 0 auto;
      background-color: skyblue;
      display: flex;
      justify-content: center;
      align-items: center;
      position: relative;
    }

    form {
      width: 450px;
      
      display: flex;
      flex-direction: column;
      padding: 20px;
      border: 3px solid pink;
      border-radius: 15px;
      margin: 30px auto;
      padding-top: 50px;
      position: relative;
    }

    form>label {
      height: 50px;
      font-size: 22px;
    }

    form>label>input {
      padding-left: 22px;
      font-size: 22px;
    }

    form>input {
      font-size: 18px;
    }

    form>span {
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      top: 10px;
      color: red;
      display: none;
    }

    form>span.active {
      display: block;
    }

    form>p {
      font-size: 18px;
    }

    /*a {
          text-decoration: none;
        }*/
  </style>
</head>

<body>

  <body>
    <div class="registration-container">
      <h1>
        <div class="header container">注册界面</div>
      </h1>
      <form id="registerForm">
        <span class="error" 用户名错误或密码错误!></span>
        <label for="username">用&nbsp;&nbsp;户&nbsp;&nbsp;名：<input type="text" id="username" required></label>
        <label for="password">密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码：<input type="password" id="password" required></label>
        <label for="confirmPassword">确认密码：<input type="password" id="confirmPassword" required></label>
        <label for="nickname">昵&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;称：<input type="text" id="nickname" required></label>
        <p>已有账号，<a href="login.html">直接登录</a></p>
        <input type="submit" value="注&nbsp;&nbsp;&nbsp;&nbsp;册">
      </form>
    </div>
    <!-- <script src="../js/register.js"></script> -->
  </body>
  <script>
    document.addEventListener('DOMContentLoaded', function () {
    var registerForm = document.getElementById('registerForm');
    registerForm.addEventListener('submit', function (e) {
      e.preventDefault();
      var username = document.getElementById('username').value;
      var password = document.getElementById('password').value;
      var confirmPassword = document.getElementById('confirmPassword').value;
      var nickname = document.getElementById('nickname').value;

      if (password!== confirmPassword) {
        alert('两次密码不一致，请重新输入');
        return;
      }

      // 检查用户名是否已存在
      var users = JSON.parse(localStorage.getItem('users')) || [];
      for (var i = 0; i < users.length; i++) {
        if (users[i].username === username) {
          alert('用户名已存在');
          return;
        }
      }

      // 将新用户数据存储到本地存储
      users.push({
            username: username,
            password: password,
            nickname: nickname
        });
        localStorage.setItem('users', JSON.stringify(users));

      alert('恭喜注册成功，点击确定跳转登录页面！');
      window.location.href = 'login.html';
    });
  });
  </script>
</body>

</html>